﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrKeHoachSP : DevComponents.DotNetBar.Office2007Form
    {
        public FrKeHoachSP()
        {
            InitializeComponent();
        }
        private string MaKH1, TenKH1, MaSP1, TenSP1, Thang1,child=" - ";
        private double SLSP, TongSL;
        private void combo_load()
        {
            
            FormProcess Frp = new FormProcess();
            string sql_SanPham = @"select MaSP,TenSP from dbo.SanPham";
            Frp.cb1(cb_MaSP, sql_SanPham, 1,0);
            
        }

        private void FrKeHoachMG_Load(object sender, EventArgs e)
        {
            combo_load();
        }
        private void insert_grid()
        {
            try
            {
                Process prs = new Process();
                if (txt_MaKH.Text == "" || txt_SoLuongSP.Text == "" || txt_TenKH.Text == "" || txt_Thang.Text == "" || cb_MaSP.Text == "")
                {
                    MessageBox.Show("Nhập thiếu thông tin");
                }
                else
                {
                    MaKH1 = txt_MaKH.Text.Trim();
                    TenKH1 = txt_TenKH.Text.Trim();
                    MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                    TenSP1 = prs.substring_left(cb_MaSP.Text, child);
                    SLSP = double.Parse(txt_SoLuongSP.Text);
                    Thang1 = txt_Thang.Text.Trim();
                    
                    if (MaSP1 == "" || TenSP1 == "")
                        MessageBox.Show("Nhập sai thông tin trong ComboBox");
                    else
                    {
                        int i = 0;
                        while (i < dataGridView1.Rows.Count - 1)
                        {
                            if (MaSP1 == dataGridView1.Rows[i].Cells["MaSP"].Value.ToString())
                                break;
                            i++;
                        }
                        if (i == dataGridView1.Rows.Count - 1)
                        {
                            dataGridView1.Rows.Add(MaKH1, TenKH1, MaSP1, TenSP1, SLSP, Thang1);
                            TongSL = double.Parse(lb_TongSP.Text);
                            TongSL += SLSP;
                            lb_TongSP.Text = TongSL.ToString();
                            txt_MaKH.Enabled = false;
                            txt_TenKH.Enabled = false;
                        }
                        else
                            MessageBox.Show("SP này đã tồn tại");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_insert_Click(object sender, EventArgs e)
        {
            insert_grid();
            int j = dataGridView1.Rows.Count - 1;
            dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells["MaKeHoach"];
            dataGridView1.FirstDisplayedCell = dataGridView1.Rows[j].Cells["MaKeHoach"];
            cb_MaSP.Focus();
        }

        private void txt_SoLuongSP_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SoLuongSP);
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridView1.CurrentCell.RowIndex;
                    TongSL= double.Parse(lb_TongSP.Text) - double.Parse(dataGridView1.Rows[r].Cells["SoLuongSP"].Value.ToString());
                    lb_TongSP.Text = string.Format("{0:###,###.#########}",TongSL);
                    dataGridView1.Rows.RemoveAt(r);
                    int j = dataGridView1.Rows.Count - 1;
                    dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells["MaKeHoach"];
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (dataGridView1.Rows.Count == 1)
            {
                lb_TongSP.Text = "0";
                txt_MaKH.Enabled = true;
                txt_TenKH.Enabled = true;
            }
        }
        private void Save()
        {
            int i;
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                for (i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                {
                    MaKH1 = dataGridView1.Rows[i].Cells["MaKeHoach"].Value.ToString();
                    TenKH1 = dataGridView1.Rows[i].Cells["TenKH"].Value.ToString();
                    SLSP = double.Parse(dataGridView1.Rows[i].Cells["SoLuongSP"].Value.ToString());
                    MaSP1 = dataGridView1.Rows[i].Cells["MaSP"].Value.ToString();
                    Thang1 = dataGridView1.Rows[i].Cells["KeHoachThang"].Value.ToString();
                    string insert_KHMG = @"insert into dbo.KeHoachSP(MaKeHoach,TenKeHoach,MaSP,SoLuongSP,KeHoachThang) values('";
                    insert_KHMG += MaKH1 + "',N'" + TenKH1 + "',N'" + MaSP1 + "'," + SLSP+ ",'" + Thang1 +"')";
                    cmd.CommandText = insert_KHMG;
                    cmd.ExecuteNonQuery();
                    dataGridView1.Rows.RemoveAt(i);
                }
                MessageBox.Show("Dữ liệu được lưu thành công");
                lb_TongSP.Text = "0";
                txt_MaKH.Enabled = true;
                txt_TenKH.Enabled = true;
                txt_TenKH.Text = "";
                txt_Thang.Text = "";
                txt_MaKH.Text = "";
            }
            catch (Exception ex)
            {
                /*string delete_Serial = @"delete from dbo.Serial_NKGo where Serial_NK=" + Serial1;
                cmd.CommandText = delete_Serial;
                cmd.ExecuteNonQuery();*/
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show("Đã xảy ra lỗi, Dữ liệu cập nhật không thành công \n" + ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_Lưu_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                {
                    dataGridView1.Rows.RemoveAt(i);
                }
                lb_TongSP.Text = "0";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }     
        }

        private void FrKeHoachSP_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

    }
}

